package com.doujiang.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.doujiang.domain.Favorite;
import com.doujiang.domain.dto.jhjNameCountDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 收藏/想看Mapper接口
 * 
 * @author 王伟舰
 * @date 2025-07-19
 */
@Repository
@Mapper
public interface jhjFavoriteMapper extends BaseMapper<Favorite>
{
    /**
     * 查询收藏/想看
     * 
     * @param id 收藏/想看主键
     * @return 收藏/想看
     */
    public Favorite selectFavoriteById(Long id);

    /**
     * 查询收藏/想看列表
     * 
     * @param favorite 收藏/想看
     * @return 收藏/想看集合
     */
    public List<Favorite> selectFavoriteList(Favorite favorite);

    /**
     * 新增收藏/想看
     * 
     * @param favorite 收藏/想看
     * @return 结果
     */
    public int insertFavorite(Favorite favorite);

    /**
     * 修改收藏/想看
     * 
     * @param favorite 收藏/想看
     * @return 结果
     */
    public int updateFavorite(Favorite favorite);

    /**
     * 删除收藏/想看
     * 
     * @param id 收藏/想看主键
     * @return 结果
     */
    public int deleteFavoriteById(Long id);

    /**
     * 批量删除收藏/想看
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFavoriteByIds(Long[] ids);
    @Select("""
    SELECT m.actors, m.title
    FROM favorite f
    JOIN movie m ON f.movie_id = m.id
    WHERE f.deleted = 0 AND m.deleted = 0
""")
    List<Map<String, String>> getFavoritedActorsAndTitles();

    @Select("""
    SELECT
        m.director AS name,
        COUNT(*) AS count,
        GROUP_CONCAT(DISTINCT m.title ORDER BY f.created_at DESC SEPARATOR ', ') AS representativeWorks
    FROM
        favorite f
        JOIN movie m ON f.movie_id = m.id
    WHERE
        f.deleted = 0
        AND m.deleted = 0
        AND f.type IN (2, 3)
    GROUP BY
        m.director
    ORDER BY
        count DESC
    LIMIT 5
""")
    List<jhjNameCountDTO> getTopDirectorsByFavoriteWithWorks();
}
